package com.accountant.travelaccountant;

import java.util.ArrayList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class EditPurpose2_ListViewAdapter extends BaseAdapter {

	Views views;
	EditPurpose2 editPurpose2;
	ArrayList<String> purposeList, amountList;
	private int radioButtonselected = -1;
	private TextView textView1, textView2;
	private static String purpose, amount;

	public EditPurpose2_ListViewAdapter(EditPurpose2 editPurpose2,
			ArrayList<String> purposeList,ArrayList<String> amountList) {
		this.editPurpose2 = editPurpose2;
		this.purposeList = purposeList;
		this.amountList=amountList;
	}

	private class Views {
		TextView textView1, textView2;
		RadioButton radioButton;
	}

	@Override
	public int getCount() {
		return purposeList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return purposeList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/* Creating an inflater */
		LayoutInflater layoutInflater = editPurpose2.getLayoutInflater();
		if (convertView == null) {
			/*
			 * Create an object so that it can the references of all the
			 * controls
			 */
			views = new Views();
			/*
			 * Assigning the view of all the textviews which you want into every
			 * listview
			 */
			convertView = layoutInflater.inflate(
					R.layout.edit_purpose2_listview_adapter_listrow, null);
			/* Assigning the ids to store references */
			views.textView1 = (TextView) convertView
					.findViewById(R.id.textView1ForEditPurpose2);
			views.textView2 = (TextView) convertView
					.findViewById(R.id.textView2forEditPurpose2);
			views.radioButton = (RadioButton) convertView
					.findViewById(R.id.radioButtonforEditPurpose2);
			/* Assigning the object to the view */
			convertView.setTag(views);
			convertView.setTag(R.id.textView1ForEditPurpose2, views.textView1);
			convertView.setTag(R.id.textView2forEditPurpose2, views.textView2);
			convertView.setTag(R.id.radioButtonforEditPurpose2,
					views.radioButton);
		} else {
			/*
			 * Get view is called every time setAdapter is calling itself to add
			 * a view
			 */
			views = (Views) convertView.getTag();
		}
		/*
		 * Arraylists of hashmaps come and then every time a new hashmap is
		 * assigned
		 */
		views.textView1.setText(purposeList.get(position));
		views.textView2.setText(amountList.get(position));
		views.radioButton.setTag(position);
		views.radioButton.setChecked(position == radioButtonselected);
		
		convertView= HelperClassForActivities.addColourToListView(position,convertView,editPurpose2);

		views.radioButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				radioButtonselected = (Integer) v.getTag();
				notifyDataSetChanged();
				LinearLayout layout = (LinearLayout) v.getParent();
				textView1 = (TextView) layout
						.findViewById(R.id.textView1ForEditPurpose2);
				setTextView2((TextView) layout
						.findViewById(R.id.textView2forEditPurpose2));
				setPurpose(textView1.getText().toString());
//				setAmount(textView2.getText().toString());
			}
		});

		return convertView;
	}

	public static String getAmount() {
		return amount;
	}

	public static void setAmount(String amount) {
		EditPurpose2_ListViewAdapter.amount = amount;
	}

	public static String getPurpose() {
		return purpose;
	}

	public static void setPurpose(String purpose) {
		EditPurpose2_ListViewAdapter.purpose = purpose;
	}

	public TextView getTextView2() {
		return textView2;
	}

	public void setTextView2(TextView textView2) {
		this.textView2 = textView2;
	}

}
